//
//  DZZDisInfoController.m
//  DZZTravel
//
//  Created by 杜东坡 on 16/7/13.
//  Copyright © 2016年 Eason. All rights reserved.
//

#import "DZZDisInfoController.h"
#import "TopView.h"

@interface DZZDisInfoController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
//头部视图相关控件
@property (nonatomic, strong)TopView *topView;

//接收数据
@property (nonatomic, strong)NSMutableArray *dataSource;
@end

@implementation DZZDisInfoController

- (UIView *)topView {
    if (!_topView) {
        _topView = [[TopView alloc]initWithFrame:CGRectMake(5, 5, kWidth-40, 400)];
        
    }
    return _topView;
}



- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setHeaderView];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Icon-Small@2x"] style:UIBarButtonItemStyleDone target:self action:@selector(returnAction)];
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"DZZDiscoverInfoCell" bundle:nil] forCellReuseIdentifier:@"CellIDO"];
    
    
    [self requestData];

}
- (void)returnAction {
   
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];

}

#pragma mark ------头部视图-------
- (CGFloat)labelHeightWithText:(NSString *)text font:(UIFont *)font{
    
    NSDictionary *dic = @{NSFontAttributeName : font};
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 100000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return rect.size.height;
}


- (void)setHeaderView {
    
    //设置标题
    self.title = self.discoverModel.title;
    

    [self.topView setTopViewWithModel:self.discoverModel];
    
    CGFloat labelHeight = [self labelHeightWithText:self.topView.subTitLabel1.text font:[UIFont systemFontOfSize:12]];
    CGRect frame = self.topView.subTitLabel1.frame;
    frame.size.height = labelHeight;
    self.topView.subTitLabel1.frame = frame;

    CGRect topViewFrame = self.topView.frame;
    topViewFrame = CGRectMake(5, 5, kWidth-10,self.topView.imageView1.frame.size.height + self.topView.subTitLabel1.frame.size.height + self.topView.titleLabel1.frame.size.height + self.topView.locationLabel1.frame.size.height);
    self.topView.frame = topViewFrame;
    
    self.tableView.tableHeaderView = self.topView;
//    NSLog(@"%@",NSStringFromCGRect(self.topView.frame));

   
   
    
}


#pragma mark ------数据请求-------
- (void)requestData {
    [RequestManager fectDataWithUrl:discoverInfoUrl parameter:@{@"themeId":self.discoverModel.themeId} method:@"GET" compelet:^(NSData *data) {
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
       
        for (NSDictionary *smallDic in bigDic[@"content"][@"productList"]) {
             DZZDisInfoModel *model = [[DZZDisInfoModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.dataSource addObject:model];
//            NSLog(@"%@",smallDic);
        }
        
    } upDateUI:^{
        [self.tableView reloadData];
    }];

}

#pragma mark ------UITableViewDelegate-------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DZZDiscoverInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIDO" forIndexPath:indexPath];
    [cell setCellWithModel:self.dataSource[indexPath.row]];
    //设置无选中色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 90;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DZZProductInfoController *DIC = [[DZZProductInfoController alloc]init];
    self.navigationController.navigationBar.hidden = YES;
    
    [self.navigationController pushViewController:DIC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
